package com.clkj.nangang.mvp.index.appeal;

import android.support.annotation.NonNull;

import com.clkj.nangang.common.Constant;
import com.clkj.nangang.common.TipStr;
import com.clkj.nangang.http.HttpService;
import com.clkj.nangang.util.LogUtil;
import com.clkj.nangang.util.schedulers.BaseSchedulerProvider;
import com.google.gson.JsonObject;

import rx.Observer;
import rx.Subscription;
import rx.functions.Func1;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2017/3/1.
 */

public class AppealPresenter implements AppealContract.Presenter {

    @NonNull
    AppealContract.View mExampleView;

    @NonNull
    private final BaseSchedulerProvider mSchedulerProvider;

    @NonNull
    private final HttpService mHttpService;

    @NonNull
    private CompositeSubscription mSubscriptions;


    public AppealPresenter(@NonNull AppealContract.View exampleView, @NonNull BaseSchedulerProvider mSchedulerProvider
            , @NonNull HttpService mHttpService) {
        this.mExampleView = exampleView;
        this.mSchedulerProvider = mSchedulerProvider;
        this.mHttpService = mHttpService;
        this.mSubscriptions = new CompositeSubscription();

        this.mExampleView.setPresenter(this);
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.unsubscribe();
        mSubscriptions = null;
    }

    @Override
    public void submitAppeal(String PersonID, String Reason) {
        mExampleView.showLoading();

        Subscription subscription = mHttpService.submitAppeal(PersonID, Reason)
                .subscribeOn(mSchedulerProvider.computation())
                .observeOn(mSchedulerProvider.ui())
                .filter(new Func1<JsonObject, Boolean>() {
                    @Override
                    public Boolean call(JsonObject jsonObject) {
                        LogUtil.e("getCommunityLists" + jsonObject.toString());
                        if (!jsonObject.get(Constant.RESPONSE_KEY_RESULT).getAsString().trim().equals("success")) {
                            mExampleView.dismissLoading();
                            mExampleView.submitAppealFail("获取数据失败");
                        }

                        return jsonObject.get(Constant.RESPONSE_KEY_RESULT).getAsString().trim().equals("success");
                    }
                })
                .subscribe(new Observer<JsonObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        mExampleView.dismissLoading();
                        mExampleView.onNetError(TipStr.TIP_SERVER_GET_DATA_WRONG);
                    }

                    @Override
                    public void onNext(JsonObject jsonObject) {
                        mExampleView.dismissLoading();

                        mExampleView.submitAppealSuccess("提交成功,请等待解决");
                    }
                });
        mSubscriptions.add(subscription);
    }
}
